﻿using System;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Globalization;

namespace TPG.SharePoint.Workflow.Controls
{
    class Spacer
        : Image
    {
        const string _resourceKey = "TPG.SharePoint.Workflow.Controls.BLANK.GIF";

        public override string ImageUrl
        {
            get
            {
                RegisterWebResources();
                return DesignMode ? 
                    Page.ClientScript.GetWebResourceUrl(
                        typeof(Spacer), _resourceKey) :
                    "/_layouts/images/blank.gif"; 
            }
            set
            {
            }
        }

        public Spacer()
        {
        }

        public Spacer(int width, int height)
        {
            Width = new Unit(width);
            Height = new Unit(height);
        }

        protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Width, Width.Value.ToString(CultureInfo.InvariantCulture));
            writer.AddAttribute(HtmlTextWriterAttribute.Height, Height.Value.ToString(CultureInfo.InvariantCulture));
            writer.AddAttribute(HtmlTextWriterAttribute.Alt, "");
            writer.AddAttribute(HtmlTextWriterAttribute.Src, ImageUrl);
        }

        protected override void OnPreRender(EventArgs e)
        {
            RegisterWebResources();
            base.OnPreRender(e);
        }

        private void RegisterWebResources()
        {

            if (Page.ClientScript.IsClientScriptIncludeRegistered(
                typeof(Spacer), _resourceKey) == false)
            {
                Page.ClientScript.RegisterClientScriptResource(
                    typeof(Spacer), _resourceKey);
            }
        }
    }
}
